import { MpInfoModel } from '@/models/mpInfo'
import MpInfoTable from '@/pages/MpInfo/components/MpInfoTable'
import MpInfoUpdateModal from '@/pages/MpInfo/components/MpInfoUpdateModal'
import { MpInfoVO, saveMpInfo, updateMpInfo } from '@/services/MpInfoService'
import { useSnapshot } from '@@/exports'
import { PageContainer } from '@ant-design/pro-components'
import { ActionType } from '@ant-design/pro-table'
import { message } from 'antd'
import { useRef } from 'react'

const MpInfo = () => {
  // 表格引用
  const tableRef = useRef<ActionType>()
  let mpInfoState = useSnapshot(MpInfoModel.state)

  // 提交修改
  const onUpdateFinish = async (formData: any) => {
    let dto: MpInfoVO = JSON.parse(JSON.stringify(formData))
    // 字段处理

    let func = mpInfoState.mpInfo ? updateMpInfo : saveMpInfo
    if (await func(dto)) {
      message.success('操作成功')
      // 刷新表格
      tableRef.current?.reload()
      return true
    }
    return false
  }

  return (
    <PageContainer title="公众号信息">
      {/*公众号信息表格 开始*/}
      <MpInfoTable tableRef={tableRef} />
      {/*公众号信息表格 结束*/}

      {/*修改模态框 开始*/}
      <MpInfoUpdateModal onFinish={onUpdateFinish} />
      {/*修改模态框 结束*/}
    </PageContainer>
  )
}

export default MpInfo
